// JavaScript Document

/**
 * import compontent 
 */
Vsg.importClass(
	[
		"Vsg.DataSourceEntity.DataSourceEntityBuilder",
		"Vsg.EntityRecord.StockInfo"
	]
);

Vsg.loadExtLibs([
	"miframe-debug.js"
	]
);

function getStockInfoPanel() {
	try {
		var $dsEntity = new Vsg.DataSourceEntity.DataSourceEntityBuilder();
		$dsEntity.setUrl( UrlMKey['stk-assembly-stocks-list']+"?categoryId="+passParameters.categoryId );
		$dsEntity.setEntityRecord( new Vsg.EntityRecord.StockInfo() );	
		
		var $ds = $dsEntity.newLoadedDataSource({
			autoload:true
		});

		// --- defined the grid panel ---
    	var gridPanel = new Ext.grid.GridPanel({
			id:'stockListPanel',
        	store: $ds,
			maskDisabled:false,
        	loadMask: true,

        	// grid columns
			colModel:new Ext.grid.ColumnModel([
				{
					id:"code",
					header: "股票编码",
					width: 100,
					sortable: false,
					dataIndex: "code"
				},
				{
					header: "股票名称",
					sortable: true, 
					dataIndex: "name"
				}				
			]),

        	// customize view config
        	viewConfig: {
            	forceFit:true
        	},
			
			tbar:[{
				id:"btnSchedule",
				text : "查看数据",
				iconCls :"time-add",
				xtype:"button",
				handler:Vsg.getDefinedEvent("job-schedule.evt_add")
			}],

        	// paging bar on the bottom
        	bbar: new Ext.PagingToolbar({
            	pageSize: Ext.PAGE_SIZE,
            	store: $ds,
            	emptyMsg: "当前没有数据"
        	})
    	});
		
		// --- defined and add event ---
		gridPanel.on("rowclick" , Vsg.getDefinedEvent("CategoryStocksPanel.evt_rowclick"));		
		
		return gridPanel;	
	} catch (e) {
		alert("Get model panel error: "  );
	}	 
}

/** ===============================
 * init ui interface 
 * ================================
 */
function init(){

	try {
		

		var stockPanel = {
			id:"assoPanelId",
			xtype:"iframepanel",
			height:200,
			border:true,
			header:false,
			loadMask:true,
			frameConfig:{autoCreate:{id:"extFrame" , name:"extFrameName",frameborder:0}},
			defaultSrc:"about:blank"
		}

		
		// --- set layout content -----
		var $layoutContent = {
			layout:"border",
			items:[
				{
					region:'west',
					width:170,
					border:false,
					layout:'fit',
					split: true,
					items:[getStockInfoPanel()]
				},
				{
					region:'center',
					layout:'fit',
					border:false,
					items:[stockPanel]
				}
			]
		};

    	new Ext.Viewport( $layoutContent );


	} catch (e) {
		alert('出现系统性错误，请联系管理人员!');
	}
}
// --- run the init method
Vsg.run( init );


Vsg.definedEvent("CategoryStocksPanel.evt_rowclick" , function($this, rowIndex, event) {
	
	var record = $this.getStore().getAt(rowIndex);
	
	var stockCode = record.get("code");

	var framePanel = Ext.getCmp('assoPanelId');
	
	var url = UrlMKey['stk-daily-kline']+"?stockCode="+stockCode;
	
	framePanel.setSrc(url);

});